package com.collect.senegal.zhiScene.zhiWidget.zhiNavigationBar;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.collect.senegal.R;
import com.collect.senegal.zhiScene.zhiWidget.zhiMenuView.ZHIOnMenuItemDidChangedListener;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ZHIBottomTabBarItem extends LinearLayout implements View.OnClickListener {

    private final Context mContext;
    private View mView;
    private String tabTitle;
    private int tabTitleUnCheckColor;
    private int tabTitleDidCheckColor;
    private int tabImgUnCheck;
    private int tabImgDidChecked;

    private boolean checked = false;

    @BindView(R.id.tv_main_title)
    TextView tv_main_title;
    @BindView(R.id.iv_icon)
    ImageView iv_icon;

    private ZHIOnMenuItemDidChangedListener onMenuItemDidChangedListener;

    public ZHIBottomTabBarItem(Context context) {
        this(context, null);
    }

    public ZHIBottomTabBarItem(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZHIBottomTabBarItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        this.mContext = context;

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CFBottomTabBarItem);
        tabTitle = typedArray.getString(R.styleable.CFBottomTabBarItem_tabTitle);
        tabImgUnCheck = typedArray.getResourceId(R.styleable.CFBottomTabBarItem_tabImgUnCheck, R.drawable.ic_87);
        tabImgDidChecked = typedArray.getResourceId(R.styleable.CFBottomTabBarItem_tabImgDidChecked, R.drawable.ic_88);
        tabTitleUnCheckColor = typedArray.getColor(R.styleable.CFBottomTabBarItem_tabTitleUnCheckColor, getResources().getColor(R.color.theme_black_2));
        tabTitleDidCheckColor = typedArray.getColor(R.styleable.CFBottomTabBarItem_tabTitleDidCheckColor, getResources().getColor(R.color.theme_yellow_1));
        checked = typedArray.getBoolean(R.styleable.CFBottomTabBarItem_isChecked, false);

        typedArray.recycle();

        initView();
    }

    @Override
    public void onClick(View v) {
        setChecked(true);
        if(this.onMenuItemDidChangedListener != null){
            this.onMenuItemDidChangedListener.OnMenuItemDidChanged("");
        }
    }

    /**
     * 初始化组件数据
     * @type  method
     * @author liwenjie
     * @creatTime 2019-10-25 19:43
     */
    private void initView() {
        mView = View.inflate(mContext, R.layout.view_tabbar_item, this);
        ButterKnife.bind(this);


        tv_main_title.setText(tabTitle);

        if(checked){
            iv_icon.setImageResource(tabImgDidChecked);
            tv_main_title.setTextColor(tabTitleDidCheckColor);
        }
        else {
            iv_icon.setImageResource(tabImgUnCheck);
            tv_main_title.setTextColor(tabTitleUnCheckColor);
        }

        mView.setOnClickListener(this);
        iv_icon.setOnClickListener(this);
        tv_main_title.setOnClickListener(this);
    }

    /**
     * 设置选中状态
     * @type  method  
     * @author liwenjie
     * @creatTime 2019-11-05 12:32
     */
    public void setChecked(boolean checked) {
        this.checked = checked;

        if(checked){
            iv_icon.setImageResource(tabImgDidChecked);
            tv_main_title.setTextColor(tabTitleDidCheckColor);
        }
        else {
            iv_icon.setImageResource(tabImgUnCheck);
            tv_main_title.setTextColor(tabTitleUnCheckColor);
        }
    }

    /**
     * 设置显示内容
     * @type  method
     * @author liwenjie
     * @creatTime 2019-10-25 19:58
     */
    public void setText(String text){
        tabTitle = text;
        tv_main_title.setText(tabTitle);
    }

    /**
     * 获取显示内容
     * @type  method
     * @author liwenjie
     * @creatTime 2019-10-25 19:58
     */
    public String getText(){
        return tv_main_title.getText().toString().trim();
    }


    /**
     * 设置监听
     * @type  method
     * @author liwenjie
     * @creatTime 2019-11-04 10:15
     */
    public void setCFOnMenuItemDidChangedListener(ZHIOnMenuItemDidChangedListener onMenuItemDidChangedListener){
        this.onMenuItemDidChangedListener = onMenuItemDidChangedListener;
    }

}
